package movie.test.user;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import movie.test.as.user.service.UserInfoService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

@RestController
@RequestMapping("/userInfo")
public class UserInfoController {

	private static Logger logger = LoggerFactory
			.getLogger(UserInfoController.class);

	@Autowired
	private UserInfoService userInfoServiceImpl;

	/**
	 * 查询所有的购买电影票用户的信息
	 * 
	 * @param request
	 * @return
	 * @date 2017-03-01
	 */
	@ResponseBody
	@RequestMapping(value={"/findAllUsersList"},method={RequestMethod.GET,RequestMethod.POST})
	public Object findAllUsersList(@RequestBody JSONObject objs,HttpServletRequest request) {
		try {
			logger.debug("查询所有用户开始");
			List  listUsers = userInfoServiceImpl.findAllUsersList(objs);
			logger.info("查询所有用户结束");
			LinkedHashMap<String, Object> map = new LinkedHashMap<String, Object>();
			map.put("result", listUsers);
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 添加购买电影票的用户
	 * 
	 * @param objs
	 * @param request
	 * @return
	 * @date 2017-03-01
	 */
	@ResponseBody
	@RequestMapping(value={"/addUser"},method={RequestMethod.GET,RequestMethod.POST})
	public Object addUser(@RequestBody JSONObject objs,	HttpServletRequest request) {
		try {
			logger.debug("添加用户开始");
			Map<String, Object> map =  userInfoServiceImpl.addUser(objs);
			logger.info("添加用户结束");
			Map<String, Object> result = new LinkedHashMap<String, Object>();
			if (map.containsKey("errorcode")) {
				result.put("errorResultInfo", map);
				
			} else {
				result.put("successResultInfo", map);
				
			}
			return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
	
		return null;
	}
	/**
	 * 修改购买电影票的用户
	 * 
	 * @param objs
	 * @param request
	 * @return
	 * @date 2017-03-01
	 */
	@ResponseBody
	@RequestMapping(value={"/updateUser"},method={RequestMethod.GET,RequestMethod.POST})
	public Object updateUser(@RequestBody JSONObject objs,
			HttpServletRequest request) {
		try {
			logger.debug("修改用户开始");
			Map<String, Object> map = userInfoServiceImpl.updateUser(objs);
			logger.info("修改用户结束");
			LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>();
			if (map.containsKey("errorcode")) {
				result.put("errorResultInfo", map);
				return result;
			} else {
				result.put("successResultInfo", map);
				return result;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 查询用户
	 * 
	 * @param objs
	 * @param request
	 * @return
	 * @date 2017-03-01
	 */
	@ResponseBody
	@RequestMapping(value={"/getUserById"},method={RequestMethod.GET,RequestMethod.POST})
	public Object getUserById(@RequestBody JSONObject objs,
			HttpServletRequest request) {
		try {
			logger.debug("查询用户开始");
			Map<String, Object> map = userInfoServiceImpl.getUserById(objs);
			logger.info("查询用户结束");
			LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>();
		    result.put("result", map);
		    return result;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 删除购买电影票的用户
	 * 
	 * @param objs
	 * @param request
	 * @return
	 * @date 2017-03-01
	 */
	@ResponseBody
	@RequestMapping(value={"/deleteUser"},method={RequestMethod.GET,RequestMethod.POST})
	public Object deleteUser(@RequestBody JSONObject objs,
			HttpServletRequest request) {
		try {
			logger.debug("删除用户开始");
			Map<String, Object> map = userInfoServiceImpl.deleteUser(objs);
			logger.info("删除用户结束");
			LinkedHashMap<String, Object> result = new LinkedHashMap<String, Object>();
			if (map.containsKey("errorcode")) {
				result.put("errorResultInfo", map);
				return result;
			} else {
				result.put("successResultInfo", map);
				return result;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	/**
	 * 修改密码
	 * 
	 * @param objs
	 * @param request
	 * @return
	 * @date 2017-03-01
	 */
	@ResponseBody
	@RequestMapping(value={"/updatePasswordById"},method={RequestMethod.GET,RequestMethod.POST})
	public Object updatePasswordById(@RequestBody JSONObject objs,
			HttpServletRequest request) {
		try {
			logger.debug("修改用户密码开始");
		     int count = userInfoServiceImpl.updatePasswordById(objs);
			logger.info("修改用户密码结束");
			LinkedHashMap<String,Object> map = new LinkedHashMap<String,Object>();
			map.put("success", count);
            return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
  
}
